﻿using System.Collections.Generic;

namespace GeometryServer.Services
{
    public static class Compute
    {
        public static dynamic ConvexHull(DotSpatialGeometryArray Geometries)
        {
            /*
            if (geometries is DotSpatial.Topology.MultiPoint)
            {
                return (DotSpatial.Topology.Polygon)geometries.ConvexHull();
            }
            else if (geometries is List<DotSpatial.Topology.Point>)
            {
                var c = new List<DotSpatial.Topology.Coordinate>();
                for (int i = 0; i < geometries.Count; i++)
                {
                    c.Add(new DotSpatial.Topology.Coordinate(geometries[i].X, geometries[i].Y));
                }
                var mutiPoint = new DotSpatial.Topology.MultiPoint(c);
                var convexHull = (DotSpatial.Topology.Polygon)mutiPoint.ConvexHull();
                return convexHull;
            }
            return null;
            */
            var coordinateList = new List<DotSpatial.Topology.Coordinate>();
            var geometries = Geometries.Geometries;
            for (int i = 0; i < geometries.Count; i++)
            {
                var coord = (geometries[i]).Coordinates;
                coordinateList.AddRange(coord);
            }
            return (new DotSpatial.Topology.MultiPoint(coordinateList).ConvexHull());
        }

        public static GISServer.Core.Client.GeometryService.AreasAndLengths AreasAndLengths(List<DotSpatial.Topology.MultiPolygon> PolygonArray)
        {
            var result = new GISServer.Core.Client.GeometryService.AreasAndLengths();

            for (int i = 0; i < PolygonArray.Count; i++)
            {
                result.Areas.Add(PolygonArray[i].Area);
                result.Lengths.Add(PolygonArray[i].Length);
            }
            return result;
        }

        public static GISServer.Core.Client.GeometryService.LabelPoints LabelPoints(List<DotSpatial.Topology.MultiPolygon> PolygonArray)
        {
            var result = new GISServer.Core.Client.GeometryService.LabelPoints();
            for (int i = 0; i < PolygonArray.Count; i++)
            {
                var envelope = PolygonArray[i].EnvelopeAsGeometry;
                var centroid = envelope.Centroid;
                result.labelPoints.Add(new GISServer.Core.Client.Geometry.Point(centroid.X, centroid.Y));
            }
            return result;
        }

        public static GISServer.Core.Client.GeometryService.Lengths Lengths(List<DotSpatial.Topology.MultiLineString> MultiLineStringArray)
        {
            var result = new GISServer.Core.Client.GeometryService.Lengths();

            for (int i = 0; i < MultiLineStringArray.Count; i++)
            {
                result.lengths.Add(MultiLineStringArray[i].Length);
            }
            return result;
        }

        public static dynamic Union(Services.DotSpatialGeometryArray geometriesarray)
        {
            dynamic result = null;
            var geometries = geometriesarray.Geometries;
            for (int i = 0; i < geometries.Count; i++)
            {
                if (result != null)
                {
                    result.Union(geometries[i]);
                }
                else
                {
                    result = geometries[i];
                }
            }
            return result;
        }

        public static dynamic Intersection(Services.DotSpatialGeometryArray geometryarray, Services.DotSpatialSingleGeometry singlegeometry)
        {
            dynamic result = null;
            var geometries = geometryarray.Geometries;
            var geometry = singlegeometry.Geometry;
            var list=new List<DotSpatial.Topology.IGeometry>();
            for (int i = 0; i < geometries.Count; i++)
            {
                list.Add(geometries[i].Intersection(geometry));
            }
            return result;
        }

        public static GISServer.Core.Client.GeometryService.Distance Distance(Services.DotSpatialSingleGeometry singlegeometry1, Services.DotSpatialSingleGeometry singlegeometry2)
        {
            var result = new GISServer.Core.Client.GeometryService.Distance();
            result.distance = (singlegeometry1.Geometry).Distance(singlegeometry2.Geometry);
            return result;
        }

        public static dynamic Buffer(DotSpatialGeometryArray dotspatial_geometries, double bufferdistance)
        {
            var result = new DotSpatialGeometryArray();
            result.GeometryType = "esriGeometryPolygon";
            result.Geometries = new List<DotSpatial.Topology.MultiPolygon>();
            for (int i = 0; i < dotspatial_geometries.Geometries.Count; i++)
            {
                result.Geometries.Add(new DotSpatial.Topology.MultiPolygon(dotspatial_geometries.Geometries[i].Buffer(bufferdistance)));
            }
            return result;

        }
    }
}